<?php

/**
 * Description of G_Files
 * @author 耿鸿飞<eoe2005@qq.com>
 */
class G_FileLog extends GC_Log{
    protected $_logsDir;
    protected $_files = array();
    public function __construct($dir){
        $this->_logsDir = $dir;
    }
    /**
     * 写日志
     * @param type $data
     * @param type $type
     */
    public function _write($data, $type = self::LOG) {
        $str = sprintf("[%d]",date("Y-m-d H:i:s"));
        if($type == self::SQL){//记录sql的日志
            $len = count($data);
            if($len > 0){
                if($len == 1){
                    $str .= $data[0];
                    @file_put_contents($this->getFile('sql'), $str."\n" ,FILE_APPEND);
                }else{
                    $str .= $data[1].' ERROR:'.$data[2].'['.$data[1].']';
                    @file_put_contents($this->getFile('sql_err'), $str."\n" ,FILE_APPEND);
                }
                @file_put_contents($this->getFile('sql_all'), $str."\n" ,FILE_APPEND);
            }
        }else{
            foreach($data AS $k => $val){
                if(is_array($val)){
                    $data[$k] = json_encode($val);
                }else{
                    $data[$k] = $val;
                }
            }
            $str .= implode("\t", $data);
            $fliename = $this->getFile($type);
            if($type == self::DEBUG){
                $fliename = $this->getFile('debug');
            }elseif ($type == elf::ERROR){
                $fliename = $this->getFile('error');
            }elseif($type == self::LOG){
                $fliename = $this->getFile('log');
            }
            @file_put_contents($fliename, $str."\n",FILE_APPEND);
        }
    }   
    /**
     * 获取文件的名字
     * @param type $filename
     * @return type
     */
    protected function getFile($filename){
        if(isset($this->_files[$filename]) === false){
            $this->_files[$filename] = sprintf("%s%s_%s.log",$this->_logsDir,$filename,date("Y_m_d"));
        }
        return $this->_files[$filename];
    }
}

?>
